﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kimgwajang.MidnightPeach.MidnightPeachServiceReference;
using Kimgwajang.MidnightPeach.Properties;
using Kimgwajang.MidnightPeach.Generators;
using Kimgwajang.MidnightPeach.Schema;

namespace Kimgwajang.MidnightPeach.Forms
{
    public partial class ExceptionForm : Form
    {
        private readonly Exception _exception;

        public ExceptionForm()
        {
            InitializeComponent();
        }

        public ExceptionForm(Exception exception) : this()
        {
            _exception = exception;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            StringBuilder builder = new StringBuilder();

            builder.AppendLine(_exception.Message);

            UnsupportedMacroException macroException = _exception as UnsupportedMacroException;
            if (macroException != null)
            {
                builder.AppendLine();
                builder.AppendLine("Generator : " + macroException.Generator.GetType());
                builder.AppendLine("Loop Type : " + macroException.Macro.LoopType);
                builder.AppendLine("Seperator : " + macroException.Macro.Seperator);
                builder.AppendLine("Value : " + macroException.Macro.Value);
                builder.AppendLine("Text : " + macroException.Text);
            }

            txtMessage.Text = builder.ToString();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            MidnightPeachService service = new MidnightPeachService();
            service.ReportExceptionCompleted += MidnightPeachService_ReportExceptionCompleted;
            service.ReportExceptionAsync(_exception.Message, _exception.StackTrace);
        }

        private void MidnightPeachService_ReportExceptionCompleted(object sender, AsyncCompletedEventArgs e)
        {
            MessageBox.Show(Resources.ExceptionSent);
            Application.Exit();
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
